package com.zhr.tea.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.zhr.tea.R;
import com.zhr.tea.entity.DingZhiBean;
import com.zhr.tea.utils.IsLogin;
import com.zhr.tea.view.adapter.DzAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class YCDZActivity extends AppCompatActivity {
    public static final String YUANCHA_URL = "http://120.55.137.143:54321/service/teabubble/diy?methods=sceneList";

    @BindView(R.id.lv_ycdz)
    RecyclerView lvYcdz;
    private List<DingZhiBean.ContentBean> data;
    private DzAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ycdz);
        initView();
        initData();
    }

    public void initView() {
        ButterKnife.bind(this);
        data = new ArrayList<>();
        adapter = new DzAdapter(this, data, new DzAdapter.OnItemClick() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(YCDZActivity.this, YuanChaWebActivity.class);
                intent.putExtra("url", data.get(position).getDetailUrl());
                startActivity(intent);
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(this);
        lvYcdz.setLayoutManager(manager);
        lvYcdz.setAdapter(adapter);
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("beginId", "0");
        obj.put("count", "20");
        obj.put("phoneNum", IsLogin.user != null ? IsLogin.user.getBody().getMemberAccount() : "");
        params.applicationJson(obj);
        HttpRequest.post(YUANCHA_URL, params, new BaseHttpRequestCallback<DingZhiBean>() {
            @Override
            protected void onSuccess(DingZhiBean dingZhiBean) {
                super.onSuccess(dingZhiBean);
                data.addAll(dingZhiBean.getContent());
                adapter.notifyDataSetChanged();
            }
        });
    }
}
